//
//  SelectStep3ViewController.m
//  OutesIOS
//
//  Created by norman on 2019/1/13.
//  Copyright © 2019 norman. All rights reserved.
//

#import "SelectStep3ViewController.h"
#import "BigMachine_TabViewController.h"
#import "CustomActivity.h"

@interface SelectStep3ViewController ()

@property (nonatomic, strong) ReportViewController *selectionReport;

@property (nonatomic,strong) ReportViewController *feeReport;

@property (nonatomic,strong) UISegmentedControl *segmentedControl;


@end

@implementation SelectStep3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self setupActionBtnView];
    [self setupSegmentView];
    [self setupChildViewControllers];
    // Do any additional setup after loading the view.
}

- (void)initData {
    
}

- (void)setupChildViewControllers {
    _selectionReport = [[ReportViewController alloc] init];
    _selectionReport.url = [Tools getResourceFullPathURL:self.report1];
    if (iPhone_X) {
        _selectionReport.rate = 0.62;
    } else {
        _selectionReport.rate = 0.56;
    }
    
    [self addChildViewController:_selectionReport];
    
    _feeReport = [[ReportViewController alloc] init];
    _feeReport.url = [Tools getResourceFullPathURL:self.report2];
    if (iPhone_X) {
        _feeReport.rate = 0.52;
    } else {
        _feeReport.rate = 0.46;
    }
    [self addChildViewController:_feeReport];
}

- (void)setupActionBtnView {
    UIView *actionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, kNavigation_Bar_Height)];
    
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    [shareBtn setTintColor:[UIColor whiteColor]];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize: kListTitleFontSize];
    shareBtn.frame = CGRectMake(0, 0, [Tools sizeOfSingleLineText:@"分享" withSize:kListTitleFontSize].width + 5, [Tools sizeOfSingleLineText:@"分享" withSize:kListTitleFontSize].height + 5);
    [shareBtn addTarget:self action:@selector(shareButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [actionView addSubview:shareBtn];
    shareBtn.centerY = kNavigation_Bar_Height/2;
//    shareBtn.hidden = YES;
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [closeBtn setTintColor:[UIColor whiteColor]];
    closeBtn.titleLabel.font = [UIFont systemFontOfSize: kListTitleFontSize];
    closeBtn.frame = CGRectMake(CGRectGetMaxX(shareBtn.frame) + 5, 0, [Tools sizeOfSingleLineText:@"分享" withSize:kListTitleFontSize].width + 5, [Tools sizeOfSingleLineText:@"分享" withSize:kListTitleFontSize].height + 5);
    [closeBtn addTarget:self action:@selector(onBackToProject) forControlEvents:UIControlEventTouchUpInside];
    [actionView addSubview:closeBtn];
    closeBtn.centerY = kNavigation_Bar_Height/2;
    
    actionView.width = CGRectGetMaxX(closeBtn.frame);
    
    
    //    NSArray *imageList = @[[UIImage imageNamed:@"menuChat.png"], [UIImage imageNamed:@"menuUsers.png"],[UIImage imageNamed:@"menuClose.png"]];
    //    sideBar = [[CDSideBarController alloc] initWithImages:imageList];
    //    sideBar.delegate = self;
    //    [sideBar insertMenuButtonOnView:actionView atPosition:CGPointMake(0, 0)];
    
    //自定义按钮
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:actionView];
    self.navigationItem.rightBarButtonItem = backBtn;
}

- (void)shareButtonClicked {
    NSString *shareText = self.project_name;
    UIImage *shareImage = [UIImage imageNamed:@"share_icon"];
    NSURL *url;
    if (_segmentedControl.selectedSegmentIndex == 0) {
        url = _selectionReport.url;
    } else {
        url = _feeReport.url;
    }
    if ([Tools isEmpty:shareText]) {
        shareText = @"集中供暖报告";
    }
//    NSString* webStringURL = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSURL *shareUrl = [NSURL URLWithString:webStringURL];
    NSArray *activityItemsArray = @[shareText,shareImage,url];
    
    // 自定义的CustomActivity，继承自UIActivity
    CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle:shareText ActivityImage:[UIImage imageNamed:@"share_icon"] URL:url ActivityType:@"Custom"];
    //    CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle: shareText ActivityImage:[UIImage imageNamed:@"custom.png"] URL: shareUrl ActivityType:@"Custom" ShareContext:activityItemsArray];
    NSArray *activityArray = @[customActivity];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:activityArray];
    activityVC.modalInPopover = YES;
    
    UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        NSLog(@"activityType == %@",activityType);
        if (completed == YES) {
            NSLog(@"completed");
        }else{
            NSLog(@"cancel");
        }
    };
    activityVC.completionWithItemsHandler = itemsBlock;
    
    [self presentViewController:activityVC animated:YES completion:nil];
}

- (void)onBackToProject{
    //返回到首页
    for (UIViewController *tmp in self.navigationController.viewControllers) {
        if ([tmp isKindOfClass:[BigMachine_TabViewController class]]) {
            [(BigMachine_TabViewController*)tmp loadData];
            [self.navigationController popToViewController:tmp animated:YES];
        }
    }
}

- (void)setupSegmentView {
    NSArray * _titles = @[@"项目表", @"运行费用表"];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:_titles];
    _segmentedControl.selectedSegmentIndex = 0;
    _segmentedControl.tintColor = [UIColor whiteColor];
    
    //修改字体的默认颜色与选中颜色
    //选择后的字体颜色（在NSDictionary中 可以添加背景颜色和字体的背景颜色）
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],
                         NSForegroundColorAttributeName,
                         [UIFont systemFontOfSize:12],
                         NSFontAttributeName,nil];
    
    [ _segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
    //默认字体颜色
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys: CM_BLUE_COLOR,
                          NSForegroundColorAttributeName,
                          [UIFont systemFontOfSize:12],
                          NSFontAttributeName,nil];
    
    [_segmentedControl setTitleTextAttributes:dic1 forState:UIControlStateNormal];
    [_segmentedControl addTarget:self action:@selector(switchView) forControlEvents:UIControlEventValueChanged];
    
    //将segmentedControl添加到导航条上
    self.navigationItem.titleView = _segmentedControl;
    
    [_segmentedControl setSelectedSegmentIndex:0];
    @weakObj(self);
    dispatch_async(dispatch_get_main_queue(), ^{
        [Weakself switchView];
    });
}

- (void)switchView {
    //获得对应的控制器
    if (self.childViewControllers.count > self.segmentedControl.selectedSegmentIndex) {
        UIViewController *vc = self.childViewControllers[self.segmentedControl.selectedSegmentIndex];
        [self.view addSubview:vc.view];
    }
}

@end
